package com.cityeye.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.Order;

import com.cityeye.entity.Department;
import com.cityeye.entity.Mmessage;
import com.cityeye.entity.Patrol;
import com.cityeye.search.SearchCriteria;

public class DepartmentDao extends AbstractEntityDao<Department>{

	public static final int TYPE_SHI=0;
	public static final int TYPE_QU=1;
	public static final int TYPE_JIEDAO=2;
	
	private static SearchCriteria[] criteria;

	protected Object evalueateProperty(String key, Object valueObj){
		if(key.equalsIgnoreCase("department")){
			String idStr = valueObj.toString();
			return sf.getCurrentSession().get(Department.class,Integer.parseInt(idStr));
		}
		
		return valueObj;
	}
	
	public Department createNewEmptyEntity() {
		return new Department();
	}
	
	public List<Department> loadChildren(Department dept) {
		List<Department> children= new ArrayList<Department>();
		
		Department dbDept = (Department)sf.getCurrentSession().get(
				getEntityClass(), dept.getId());	
		
		if(dbDept!=null){
			Set childset= dbDept.getDepartments();
			children.addAll(childset);
		}
		
		return children;
	}
	
	public List<Department> loadDept(Department dept) {
		List<Department> children= new ArrayList<Department>();
		
		Department dbDept = (Department)sf.getCurrentSession().get(
				getEntityClass(), dept.getId());	
		
		if(dbDept!=null){
			Set childset= dbDept.getDepartments();
			children.addAll(childset);
		}
		
		return children;
	}	
	
	public List<Patrol> loadPatrols(Department dept) {
		List<Patrol> result= new ArrayList<Patrol>();
		
		Department dbDept = (Department)sf.getCurrentSession().get(
				getEntityClass(), dept.getId());
		
		if(dbDept!=null){
			Set patrols= dbDept.getPatrols();
			result.addAll(patrols);
		}
		
		return result;
	}
	
	public List<Patrol> loadUsers(Department dept) {
		List<Patrol> result= new ArrayList<Patrol>();
		
		Department dbDept = (Department)sf.getCurrentSession().get(
				getEntityClass(), dept.getId());
		
		if(dbDept!=null){
			Set patrols= dbDept.getPatrols();
			result.addAll(patrols);
		}
		
		return result;
	}
	
	public List<Mmessage> loadMMessage(Department dept,int type) {
		List<Mmessage> result= new ArrayList<Mmessage>();
		
		Department dbDept = (Department)sf.getCurrentSession().get(
				getEntityClass(), dept.getId());
		
		if(dbDept!=null){
			Set patrols= dbDept.getMmessages();
			result.addAll(patrols);
		}
		
		return result;
	}	
	
	/**
	 * @param type 0: shi; 1: qu; 2: Jiedao;
	 * @return
	 */
	public List<Department> getDepartmentByType(int type) {
		List<Department> result= this.findByProperty(Department.class, "TYPE", type);
		return result;
	}	

	public SearchCriteria[] getSearchCriteria() {
		if (criteria == null) {
			criteria = new SearchCriteria[] {
					new SearchCriteria("Code", SearchCriteria.VT_INT),
					new SearchCriteria("Name", SearchCriteria.VT_STRING),
					new SearchCriteria("Taxable", SearchCriteria.VT_STRING),
					new SearchCriteria("Mobile", SearchCriteria.VT_STRING) };
		}
		return criteria;
	}

	
	protected Class<Department> getEntityClass(){
		return Department.class;
	}
	
	protected Order getOrderField(){
		return Order.asc("name");
	}	
}
